package com.service.medical.Impl;

import com.comm.R;
import com.comm.vo.SupperlierVo;
import com.entity.Supplier;
import com.github.pagehelper.PageHelper;
import com.mapper.medical.SupplierMapper;
import com.service.medical.SupplierService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public R findSupList(int currentPage,int pageSize,String supName, String contact) {
        R r = new R();
        try {
            com.github.pagehelper.Page<Supplier> page = PageHelper.startPage(currentPage,pageSize);
            List<Supplier> supplierList = supplierMapper.findSupList(supName,contact);
            long count = page.getTotal();
            System.out.println("总记录数:"+count);
            if(count>0){
                r.setCode(200);
                r.setData(supplierList);
            }else {
                r.setCode(500);
                r.setData("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }


    @Override
    public R findSupById(int id) {
        R r = new R();
        try {
            List<Supplier> sup = supplierMapper.findSupById(id);
            System.out.println(sup.size());
            int count = sup.size();
            if(count>0){
                r.setCode(200);
                r.setData(sup);
            }else {
                r.setCode(500);
                r.setData("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }


    @Override
    public R addSup(Supplier supplier) {
        R r = new R();
        try {
            Integer result = supplierMapper.addSup(supplier);
            if(result>0){
                r.setCode(200);
                r.setData("success");
            }else {
                r.setCode(500);
                r.setData("新增失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");

        }
        return r;
    }

    @Override
    public R updateSup(Supplier supperlier) {
        R r = new R();
        try {
            Integer result = supplierMapper.updateSup(supperlier);
            if(result>0){
                r.setCode(200);
                r.setData("success");
            }else {
                r.setCode(500);
                r.setData("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }
    @Override
    public R deleteSup(int id) {
        R r = new R();
        try {
            Integer result = supplierMapper.deleteSup(id);
            if(result>0){
                r.setCode(200);
                r.setData("success");
            }else {
                r.setCode(500);
                r.setData("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }
}
